/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2011
Version 0.1
=======================================================*/
DeviceWanAdvanceWindow = function(device,parentWindow) {
	this.mode;
	this.commands;
	this.device = device;
	this.parentWindow = parentWindow;

    this.formIPCheck = new Ext.form.Checkbox({boxLabel:"Define Ip Address", style:"background:transparent;margin-left:10px;padding-top:10px;"});
    this.fieldIP = new Ext.form.TextField({fieldLabel:"Ip Address", width:120, style:"margin-left:10px;"});
    this.formIP = new Ext.FormPanel({labelWidth: 170, items: [this.fieldIP],border: false, disabled: true, bodyStyle:"background:transparent;margin-left:20px;"});

    this.formGatewayCheck = new Ext.form.Checkbox({boxLabel:"Define Gateway", style:"background:transparent;margin-left:10px;padding-top:10px;"});
    this.fieldGateway = new Ext.form.TextField({fieldLabel:"Gateway", width:120, style:"margin-left:10px;"});
    this.formGateway = new Ext.FormPanel({labelWidth: 170, items: [this.fieldGateway],border: false, disabled: true, bodyStyle:"background:transparent;margin-left:20px;"});

    this.formDNSCheck = new Ext.form.Checkbox({boxLabel:"Define DNS", style:"background:transparent;margin-left:10px;padding-top:10px;"});
    this.fieldDNS1 = new Ext.form.TextField({fieldLabel:"Primary DNS", width:120, style:"margin-left:10px;"});
    this.fieldDNS2 = new Ext.form.TextField({fieldLabel:"Secondary DNS", width:120, style:"margin-left:10px;"});
    this.formDNS = new Ext.FormPanel({labelWidth: 170, items: [this.fieldDNS1,this.fieldDNS2],border: false, disabled: true, bodyStyle:"background:transparent;margin-left:20px;"});

    this.formVLANCheck = new Ext.form.Checkbox({boxLabel:"edgeBOX belongs to VLAN of the Internet Service Provider", style:"background:transparent;margin-left:10px;padding-top:10px;"});
    this.fieldVLAN = new Ext.form.TextField({fieldLabel:"VLAN", width:120, style:"margin-left:10px;"});
    this.formVLAN = new Ext.FormPanel({labelWidth: 170, items: [this.fieldVLAN],border: false, disabled: true, bodyStyle:"background:transparent;margin-left:20px;"});

    this.formMTUCheck = new Ext.form.Checkbox({boxLabel:"Override MTU", style:"background:transparent;margin-left:10px;padding-top:10px;"});
    this.fieldMTU = new Ext.form.TextField({fieldLabel:"Maximum Packet Size (bytes)", width:120, style:"margin-left:10px;"});
    this.formMTU = new Ext.FormPanel({labelWidth: 170, items: [this.fieldMTU],border: false, disabled: true, bodyStyle:"background:transparent;margin-left:20px;"});

	DeviceWanAdvanceWindow.superclass.constructor.call(this, {
        title: "Advance WAN Settings",
        iconCls: "wan-device-icon",
        autoHeight: true,
        width: 350,
        resizable: false,
        plain:true,
        modal: true,
        y: 100,
        autoScroll: false,
        closeAction: "hide",

        buttons:[{
            text: "Ok",
            handler: this.onOk,
            scope: this
        },{
            text: "Cancel",
            handler: this.hide.createDelegate(this, [])
        }],

        items: [this.formIPCheck,this.formIP,this.formGatewayCheck,this.formGateway,this.formDNSCheck,this.formDNS,this.formVLANCheck,this.formVLAN,this.formMTUCheck,this.formMTU]
    });

	this.formIPCheck.on("check",this.onCheckSetting,this);	
	this.formGatewayCheck.on("check",this.onCheckSetting,this);	
	this.formDNSCheck.on("check",this.onCheckSetting,this);	
	this.formVLANCheck.on("check",this.onCheckSetting,this);	
	this.formMTUCheck.on("check",this.onCheckSetting,this);	
}

Ext.extend(DeviceWanAdvanceWindow, Ext.Window, {
    
    show : function(mode,commands){
        this.mode = mode;
        this.commands = commands;
        
        if(mode == "3")
        {
        	this.formIPCheck.show();
        	this.formIP.show();
         	this.formGatewayCheck.show();
         	this.formGateway.show();
         	this.formDNSCheck.show();
         	this.formDNS.show();
       		this.formVLANCheck.show();
        	this.formVLAN.show();
        }	
        else
		{
        	this.formIPCheck.hide();
        	this.formIP.hide();
         	this.formGatewayCheck.hide();
         	this.formGateway.hide();
         	this.formDNSCheck.hide();
         	this.formDNS.hide();
       		this.formVLANCheck.hide();
        	this.formVLAN.hide();
		}

		this.formIPCheck.setValue(false);
		this.formIP.setDisabled(true);
		this.fieldIP.setValue("");
		this.formGatewayCheck.setValue(false);
		this.formGateway.setDisabled(true);
		this.fieldGateway.setValue("");
		this.formDNSCheck.setValue(false);
		this.formDNS.setDisabled(true);
		this.fieldDNS1.setValue("");
		this.fieldDNS2.setValue("");
		this.formVLANCheck.setValue(false);
		this.formVLAN.setDisabled(true);
		this.fieldVLAN.setValue("");
		this.formMTUCheck.setValue(false);
		this.formMTU.setDisabled(true);
		this.fieldMTU.setValue("");

		for(var i=0; i<this.commands.length; i++) {
			if(this.commands[i].action == "setInternetIpAddress" && this.commands[i].args[0] != "auto") {
				this.formIPCheck.setValue(true);
				this.formIP.setDisabled(false);
    			this.fieldIP.setValue(this.commands[i].args[0]);
			} else if(this.commands[i].action == "setInternetGateway" && this.commands[i].args[0] != "auto") {
				this.formGatewayCheck.setValue(true);
				this.formGateway.setDisabled(false);
    			this.fieldGateway.setValue(this.commands[i].args[0]);
    		} else if(this.commands[i].action == "setInternetDNS" && this.commands[i].args[0] != "auto") {
				this.formDNSCheck.setValue(true);
				this.formDNS.setDisabled(false);
    			this.fieldDNS1.setValue(this.commands[i].args[0]);
    			this.fieldDNS2.setValue(this.commands[i].args[1]);
    		} else if(this.commands[i].action == "setInternetVlanValue" && this.commands[i].args.length != 0) {
				this.formVLANCheck.setValue(true);
				this.formVLAN.setDisabled(false);
    			this.fieldVLAN.setValue(this.commands[i].args[0]);
    		} else if(this.commands[i].action == "setInternetMTUValue" && this.commands[i].args[0] != "0") {
				this.formMTUCheck.setValue(true);
				this.formMTU.setDisabled(false);
    			this.fieldMTU.setValue(this.commands[i].args[0]);
    		}
    	}
    	
    	DeviceWanAdvanceWindow.superclass.show.apply(this);
    },
    
    onCheckSetting : function(check,value) {
    	
    	if(check == this.formIPCheck) {
		    this.formIP.setDisabled(!value);
    	} else if(check == this.formGatewayCheck) {
		    this.formGateway.setDisabled(!value);
    	} else if(check == this.formDNSCheck) {
		    this.formDNS.setDisabled(!value);
    	} else if(check == this.formVLANCheck) {
		    this.formVLAN.setDisabled(!value);
    	} else if(check == this.formMTUCheck) {
		    this.formMTU.setDisabled(!value);
    	}
    } ,
    
    onOk : function() {
    	var valid=true;
    	
		if(this.formIPCheck.getValue() && (this.fieldIP.getValue().length == 0 || verifyIP(this.fieldIP.getValue())== false)) {
			this.fieldIP.markInvalid("Invalid IP Address!");
			valid=false;
		}
	
		if(this.formGatewayCheck.getValue() && (this.fieldGateway.getValue().length == 0 || verifyIP(this.fieldGateway.getValue())== false)) {
			this.fieldGateway.markInvalid("Invalid Gateway!");
			valid=false;
		}
	
		if(this.formDNSCheck.getValue() && (this.fieldDNS1.getValue().length == 0 || verifyIP(this.fieldDNS1.getValue())== false)) {
			this.fieldDNS1.markInvalid("Invalid Primary DNS!");
			valid=false;
		}
	
		if(this.formDNSCheck.getValue() && (this.fieldDNS2.getValue().length == 0 || verifyIP(this.fieldDNS2.getValue())== false)) {
			this.fieldDNS2.markInvalid("Invalid Secondary DNS!");
			valid=false;
		}
	
		if(this.formVLANCheck.getValue() && (this.fieldVLAN.getValue().length == 0 || verifyNumber(this.fieldVLAN.getValue(),0,4095)== false)) {
			this.fieldVLAN.markInvalid("Invalid VLAN! The value must be between 0 and 4095.");
			valid=false;
		}
		
		if(this.formMTUCheck.getValue() && (this.fieldMTU.getValue().length == 0 || verifyNumber(this.fieldMTU.getValue(),1,1500)== false)) {
			this.fieldMTU.markInvalid("Invalid packet size! The value must be between 1 and 1500.");
			valid=false;
		}
		
		if(valid) {
			var newCommands = new Array();
			
			if(this.formIPCheck.getValue()) {
				var command = new Command()
				command.action = "setInternetIpAddress";
				command.args.push(this.fieldIP.getValue());
				newCommands.push(command);
			} else {
				var command = new Command()
				command.action = "setInternetIpAddress";
				command.args.push("auto");
				newCommands.push(command);
			}

			if(this.formGatewayCheck.getValue()) {
				var command = new Command()
	    		command.action = "setInternetGateway";
	    		command.args.push(this.fieldGateway.getValue());
				newCommands.push(command);
			} else {
				var command = new Command()
				command.action = "setInternetGateway";
				command.args.push("auto");
				newCommands.push(command);
			}

			if(this.formDNSCheck.getValue()) {
	    		var command = new Command()
	    		command.action = "setInternetDNS";
	    		command.args.push(this.fieldDNS1.getValue());
	    		command.args.push(this.fieldDNS2.getValue());
				newCommands.push(command);
			} else {
				var command = new Command()
				command.action = "setInternetDNS";
				command.args.push("auto");
				newCommands.push(command);
			}

			if(this.formVLANCheck.getValue()) {
	    		var command = new Command()
	    		command.action = "setInternetVlanValue";
	    		command.args.push(this.fieldVLAN.getValue());
				newCommands.push(command);
			} else {
				var command = new Command()
				command.action = "setInternetVlanValue";
				newCommands.push(command);
			}

	    	if(this.formMTUCheck.getValue()) {
	    		var command = new Command()
	    		command.action = "setInternetMTUValue";
	    		command.args.push(this.fieldMTU.getValue());
				newCommands.push(command);
			} else {
				var command = new Command()
				command.action = "setInternetMTUValue";
				command.args.push("0");
				newCommands.push(command);
			}
	    	
	    	this.parentWindow.setAdvanceCommands(newCommands);
			this.hide();
		}    	
    }
});
